package class01;

/*
 *@title Code08_KM
 *@description   只有一种数出现K次
 *@author Lenovo
 *@version 1.0
 *@create 2023/6/6 21:34
 */

import java.util.HashMap;
import java.util.HashSet;

public class Code08_KM {

    public static  int onlyKTimes(int[] arr,int k,int m){
        int[] t = new int[32];
        //t[0]位置1出现了几个
        //t[i]位置1出现了几个
        for (int num : arr) {
            for (int i = 0; i < 32; i++) {
                t[i]+=(num>>i)&1;
            }
        }
        int ans=0;
        for(int i=0;i<32;i++){
            //在第i位,有1
            if(t[i]%m!=0){
                ans|=(1<<i);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        //int[] arr={1,1,1,3,4,3,3,4};
        //int k=2;
        //int m=3;
        int maxKinds=100;
        int range=200;
        int testTime=100000;
        int max=9;
        System.out.println("Start======================");
        for (int i = 0; i < testTime; i++) {
            //a:1~9
            int a=(int)(Math.random()*max)+1;
            int b=(int)(Math.random()*max)+1;
            int k=Math.min(a,b);
            int m=Math.max(a,b);
            //k<m
            if(k==m){
                m++;
            }
            int[] arr=randomArray(maxKinds,range,k,m);
            int ans1=test(arr,k,m);
            int ans2=onlyKTimes(arr,k,m);
            if(ans1!=ans2){
                System.out.println("出错了!!~");
            }


        }
        System.out.println("end========================");
    }
    public  static  int test(int[] arr,int k,int m){
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int num : arr) {
            if(map.containsKey(num)){
                map.put(num,map.get(num)+1);
            }else{
                map.put(num,1);
            }
        }
        for (Integer num : map.keySet()) {
            if(map.get(num)==k){
                return num;
            }
        }
        return -1;
    }
    public  static  int[] randomArray(int maxKinds,int range,int k,int m){
        int ktimeNum=randomNumber(range);
        int numKinds=(int)(Math.random()*maxKinds)+2;
        int[] arr=new int[k+(numKinds-1)*m];
        int index=0;
        for(;index<k;index++){
            arr[index]=ktimeNum;
        }

        numKinds--;
        HashSet<Integer> set=new HashSet<>();
        set.add(ktimeNum);

        while (numKinds!=0){
            int curNum=0;
            do{
                curNum=randomNumber(range);
            }while (set.contains(curNum));
            set.add(curNum);
            numKinds--;
            for (int i = 0; i < m; i++) {
                arr[index++]=curNum;
            }
        }
        //arr 填好了
        for (int i = 0; i < arr.length; i++) {
            //i位置,随机和j位置数据交换
            int j=(int) (Math.random()*arr.length);
            int tmp=arr[i];
            arr[i]=arr[j];
            arr[j]=tmp;
        }
        return arr;
    }

    public static int randomNumber(int range){
        return 1;
    }

}
